#include "InitGame.h"

InitGame::InitGame(GameState &_state):
GameRoutine(_state)
{
}

InitGame::~InitGame(void)
{
}

void InitGame::run()
{
	srand( time(NULL) );
	//customModel->bg = new GameObject(0, 0, "NONE", 800, 600);//bg created as game object - better to use sprite here
	//customModel->addPassiveObject(customModel->bg);

	//we create a background image
	customModel->bgSprite = new Sprite();
	customModel->bgSprite->src = create_bitmap(800, 600);
	clear_bitmap(customModel->bgSprite->src);
	rectfill(customModel->bgSprite->src, 0, 0, 800, 600, makecol(200 + rand()%55, 100 + rand()%90, rand()%120));
	customModel->bgSprite = *(view->addSprite(customModel->bgSprite, 1));

	customModel->dManager.target = customModel->bgSprite;

	PhysicObject *o;//temp pointer

	//walls around track
	o = new Wall( 10, *(new b2Vec2(10,10)), *(new b2Vec2(600,10)) );
	customModel->addPassiveObject(o);
	o = new Wall( 10, *(new b2Vec2(600,10)), *(new b2Vec2(790,200)) );
	customModel->addPassiveObject(o);
	o = new Wall( 10, *(new b2Vec2(790,200)), *(new b2Vec2(790,590)) );
	customModel->addPassiveObject(o);
	o = new Wall( 10, *(new b2Vec2(790,590)), *(new b2Vec2(10,590)) );
	customModel->addPassiveObject(o);
	o = new Wall( 10, *(new b2Vec2(10,590)), *(new b2Vec2(10,10)) );
	customModel->addPassiveObject(o);

	//sensor-wall as finish line
	customModel->trackTesters[0] = new TrackCoverTester( 10, *(new b2Vec2(15,335)), *(new b2Vec2(220,335)), 1);
	customModel->addPassiveObject(customModel->trackTesters[0]);
	customModel->trackTesters[1] = new TrackCoverTester( 10, *(new b2Vec2(485,265)), *(new b2Vec2(785,265)), 2);
	customModel->addPassiveObject(customModel->trackTesters[1]);
	customModel->trackTesters[1]->visible = false;

	//wall in the middle
	o = new Wall( 200, *(new b2Vec2(250,250)), *(new b2Vec2(450,350)) );
	customModel->addPassiveObject(o);

	customModel->carA = new Car();
	customModel->addActiveObject(customModel->carA);

	customModel->carB = new Car();
	customModel->addActiveObject(customModel->carB);

	//customModel->dManager.car = customModel->carA;
	customModel->dManager.addCar( customModel->carA );
	customModel->dManager.addCar( customModel->carB );

	for(unsigned int i = 0; i < 30; ++i)
	{
		o = new Barrel(50+ rand() % 700, 50 + rand()% 500 );
		customModel->addPassiveObject(o);
	}

	for(unsigned int i = 0; i < 10; ++i)
	{
		o = new Bomb(50+ rand() % 700, 50 + rand()% 500 );
		static_cast<Bomb*>(o)->myself = &customModel->addActiveObject(o);
		customModel->dManager.addBomb( static_cast<Bomb*>(o) );
	}

	fpsPointer = new FatPointer <InitGame, void>(this, METHOD(void, InitGame, printFps)); 
	fpsTimer.addEventListaner(GTimer::TIMER_EVENT, fpsPointer);
	fpsTimer.setTimer(1000, 0);
	fpsTimer.start();

	incrPointer = new FatPointer <InitGame, void>(this, METHOD(void, InitGame, incrFrames));
	controller->addEventListaner(Controller::LOOP_EVENT, incrPointer);

	customModel->title = new TextField();
	customModel->title->setText("Glider Engine Demo");
	customModel->title = (TextField *)*(view->addSprite(customModel->title, 2));
	customModel->title->x = 640;
	customModel->title->y = 5;
	customModel->title->setColor(255, 255, 255);

	customModel->fpsCounter = new TextField();
	customModel->fpsCounter->setText("0.0 fps");
	customModel->fpsCounter = (TextField *)*(view->addSprite(customModel->fpsCounter, 2));
	customModel->fpsCounter->x = 640;
	customModel->fpsCounter->y = 15;
	customModel->fpsCounter->setColor(255, 255, 255);

	customModel->p1Laps = new TextField();
	customModel->p1Laps->setText("Player 1: 0 laps");
	customModel->p1Laps = (TextField *)*(view->addSprite(customModel->p1Laps, 2));
	customModel->p1Laps->x = 670;
	customModel->p1Laps->y = 40;
	customModel->p1Laps->setColor(255, 255, 255);

	customModel->p2Laps = new TextField();
	customModel->p2Laps->setText("Player 2: 0 laps");
	customModel->p2Laps = (TextField *)*(view->addSprite(customModel->p2Laps, 2));
	customModel->p2Laps->x = 670;
	customModel->p2Laps->y = 50;
	customModel->p2Laps->setColor(255, 255, 255);

	customModel->pauseAnim = new PauseAnim();
	customModel->pauseAnim = (PauseAnim *)*(view->addSprite(customModel->pauseAnim, 2));
	customModel->pauseAnim->visible = false;

	for(unsigned int i = 0; i < 2; ++i)
	{
		controller->manager.setDetection( (GameObject *&)customModel->carA, (GameObject *&)customModel->trackTesters[i], customModel->carA->trackTestPointer, 3); 
		controller->manager.setDetection( (GameObject *&)customModel->carB, (GameObject *&)customModel->trackTesters[i], customModel->carB->trackTestPointer, 3);
	}

	customModel->explosion.loadWave("data/aexp2.wav");

	controller->swapState("running");
}

void InitGame::printFps(Event *e)
{
	//if(customModel->frames)
	//{
		std::ostringstream buff;
		buff << ( customModel->frames /(time(NULL) - customModel->timePassed) ) ;
		buff << " fps";
		customModel->fpsCounter->setText(buff.str());
		customModel->frames = 0;
		customModel->timePassed = time(NULL);
	//}
}

void InitGame::incrFrames(Event *e)
{
	customModel->frames += 1;
}